package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bnyy.common.view.FormInfoItem;
import com.bnyy.gbp.customsView.ScaleImageView;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.message.MessageManager;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.ContactUser;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/25
 **/
public class PrivateChatSettingActivity extends BaseActivityImpl implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
    @BindView(R.id.iv)
    ScaleImageView iv;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.cb_focus_or_not)
    CheckBox cbFocus;
    @BindView(R.id.tv_group_chat_manage)
    TextView tvGroupChatManage;
    @BindView(R.id.switch_button_dont_disturb)
    SwitchButton switchButtonDontDisturb;
    @BindView(R.id.switch_button_topping)
    SwitchButton switchButtonTopping;
    @BindView(R.id.switch_button_shield)
    SwitchButton switchButtonShield;
    @BindView(R.id.form_report)
    FormInfoItem formReport;
    @BindView(R.id.tv_clear_record)
    TextView tvClearRecord;

    private ContactUser contactUser;
    private int userId;
    private int roleId;
//    public static void show(Context context, ContactUser contactUser) {
////        show(context, contactUser.getId());
////    }

    public static void show(Context context, int userId, int roleId) {
        Intent intent = new Intent(context, PrivateChatSettingActivity.class);
        intent.putExtra("userId", userId);
        intent.putExtra("roleId", roleId);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_private_chat_setting;
    }

    @Override
    public String getTitleStr() {
        return "聊天设置";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        userId = getIntent().getIntExtra("userId", -1);
        roleId = getIntent().getIntExtra("roleId", -1);
        HashMap<String, Integer> params = new HashMap<>();
        params.put("id", userId);
        params.put("role_id", roleId);

        requestManager.request(requestManager.mMessageRetrofitService.getPrivateChatSetting(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ContactUser>() {
                    @Override
                    public void onSuccess(ContactUser contactUser) {
                        super.onSuccess(contactUser);

                        tvName.setText(contactUser.getUsername());
                        GlideHelper.setCircleImage(mContext, contactUser.getUser_img(), iv, R.mipmap.icon_default_header_man);

                        PrivateChatSettingActivity.this.contactUser = contactUser;

                        switchButtonDontDisturb.setChecked(contactUser.isNo_disturb());
                        switchButtonShield.setChecked(contactUser.isShield());
                        switchButtonTopping.setChecked(contactUser.isTopping());

                        cbFocus.setChecked(contactUser.isFollowed());
                    }
                });

//        ContactUser contactUser = (ContactUser) getIntent().getSerializableExtra("contactUser");
        cbFocus.setOnCheckedChangeListener(this);
        switchButtonDontDisturb.setOnCheckedChangeListener(this);
        switchButtonTopping.setOnCheckedChangeListener(this);
        switchButtonShield.setOnCheckedChangeListener(this);


        cbFocus.setOnClickListener(this);
        switchButtonDontDisturb.setOnClickListener(this);
        switchButtonTopping.setOnClickListener(this);
        switchButtonShield.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.switch_button_dont_disturb:
                edit(switchButtonDontDisturb);
                break;
            case R.id.switch_button_topping:
                edit(switchButtonTopping);
                break;
            case R.id.switch_button_shield:
                edit(switchButtonShield);
                break;
            case R.id.cb_focus_or_not:
                edit(cbFocus);
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.switch_button_dont_disturb:
                contactUser.setNo_disturb(isChecked);
                break;
            case R.id.switch_button_topping:
                contactUser.setTopping(isChecked);
                break;
            case R.id.switch_button_shield:
                contactUser.setShield(isChecked);
                break;
            case R.id.cb_focus_or_not:
                int green = ContextCompat.getColor(mContext, R.color.green_theme);
                contactUser.setFollowed(isChecked);
                cbFocus.setText(isChecked ? "已关注" : "关注");
                cbFocus.setTextColor(isChecked ? green : Color.WHITE);
                GradientDrawable bg = new GradientDrawable();
                bg.setColor(isChecked ? Color.WHITE : green);
                bg.setStroke(1, green);
                bg.setCornerRadius(360);
                cbFocus.setBackground(bg);
                break;
        }
    }

    private void edit(CompoundButton cb) {
        if (contactUser == null) {
            return;
        }
        requestManager.request(requestManager.mMessageRetrofitService.editContactUser(RequestManager.getJsonRequestBody(contactUser)),
                new BaseObserverImpl<Object>(mContext) {
                    @Override
                    public void onSuccess() {
                        super.onSuccess();
                        if (cb.getId() == R.id.switch_button_topping) {
                            MessageManager.setPrivateChatTopping(userId,userInfoManager.getLoginUserRoleId(), switchButtonTopping.isChecked());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        cb.setChecked(!cb.isChecked());
                    }

                    @Override
                    public void onOtherCode(int code) {
                        super.onOtherCode(code);
                        cb.setChecked(!cb.isChecked());
                    }
                });
    }
}




























